(function () {

    "use strict";

    var api = {
        getOrderDetailUrl: '/api/order/content',
        chgOrder: '/api/order/chgStatus',
        commentUrl: '/api/order/comment'
    };
    sm.controller('wx-order-comment-controller', ['$scope', '$http', 'Modal', 'Toast', function ($scope, $http, Modal, Toast){

        $scope.order = {};
        $scope.expressStar = 0;
        $scope.expressStarArr = [1, 2, 3, 4, 5];

        $scope.goodsComments = [];
        $scope.isSubmitting = false;

        $scope.setExpressStar = function (i) {
            $scope.expressStar = parseInt(i);
        };

        $scope.setGoodsStar = function (i, star) {
            $scope.goodsComments[i]['star'] = parseInt(star);
        };

        var search = window.location.search;
        var orderId = search.split('=')[1];

        $http.get(api.getOrderDetailUrl, {
            params: {
                orderId: orderId
            }
        }).success(function (res) {
            if (res.state) {
                $scope.order = res.data;
                angular.forEach($scope.order.goods, function (val) {
                    var goodsId = val['goodsId'];
                    var data = {
                        goodsId: goodsId,
                        goodsName: val.goodsName,
                        thumb: val.thumb,
                        star: 0,
                        message: ''
                    };
                    $scope.goodsComments.push(data);
                });
            }
        });

        /**
         * 提交
         * @returns {boolean}
         */
        $scope.commit = function () {
            var messageOk = true, starOk = true, wordFull = true;
            angular.forEach($scope.goodsComments, function (val) {
                var message = val.message;
                if (!message) {
                    messageOk = false;
                }

                if (wordCount(message) < 5) {
                    wordFull = false;
                }

                if (!val.star) {
                    starOk = false;
                }
            });

            if (!messageOk) {
                alertMsg('部分商品未填写评论信息,请填写完整');
                return false;
            }

            if (!starOk) {
                alertMsg('部分商品未打分,请对所有商品进行评价');
                return false;
            }

            if (!wordFull) {
                alertMsg('所有评论请大于 5 个字');
                return false;
            }

            if (!$scope.expressStar) {
                alertMsg('请对物流进行评价');
                return false;
            }

            Toast.loading({title: '正在提交'});
            $http.post(api.commentUrl, {
                express: $scope.expressStar,
                goods: $scope.goodsComments,
                orderId: orderId,
                anonymous: 0
            }).success(function (res) {
                Toast.stop();
                if (!res.state) {
                    Toast.error({title: '评论失败'});
                } else {
                    Toast.success({
                        title:'评论成功',
                        callback: function () {
                            location.href = '/order'
                        }
                    });
                }
            });

        };

        function alertMsg(msg){
            Modal.alert({
                'title': '操作提示',
                'content': msg,
                'buttons': [{
                    name: '确定',
                    event: function () {

                    }
                }]
            });
        }

    }]);
})();

